package cn.tedu.store.product.webapi.controller;


import cn.tedu.store.commons.pojo.product.dto.PictureAddNewDTO;
import cn.tedu.store.commons.pojo.product.dto.PictureUpdateDTO;
import cn.tedu.store.commons.pojo.product.vo.PictureListItemVO;
import cn.tedu.store.commons.pojo.product.vo.PictureStandardVO;
import cn.tedu.store.product.service.IPictureService;
import cn.tedu.store.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "08.商品图片管理")
@RestController
@RequestMapping("/pictures")
public class PictureController {

    @Autowired
    private IPictureService pictureService;

    @ApiOperation("添加图片")
    @ApiOperationSupport(order = 100)
    @PostMapping("add-new")
    public JsonResult<Void> addNew(PictureAddNewDTO pictureAddNewDTO) {
        log.debug("开始处理[添加图片]请求 参数:{}", pictureAddNewDTO);
        pictureService.addNew(pictureAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("批量添加图片")
    @ApiOperationSupport(order = 110)
    @PostMapping("add-new-batch")
    public JsonResult<Void> addNewBatch(List<PictureAddNewDTO> list) {
        log.debug("开始处理[批量添加图片]请求 参数:{}", list);
        pictureService.addNewBatch(list);
        return JsonResult.ok();
    }

    @ApiOperation("删除图片")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "图片id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> deleteById(@PathVariable Long id) {
        log.debug("开始处理[删除图片]请求 参数:{}", id);
        pictureService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("批量删除图片")
    @ApiOperationSupport(order = 210)
    @PostMapping("/delete-batch")
    public JsonResult<Void> deleteByIds(Long[] ids) {
        log.debug("开始处理[批量删除图片]请求 参数:{}", ids);
        pictureService.deleteByIds(ids);
        return JsonResult.ok();
    }

    @ApiOperation("将图片设置为封面")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "图片id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/is-cover")
    public JsonResult<Void> setIsCover(@PathVariable Long id) {
        log.debug("开始处理[设置封面]请求 参数:{}", id);
        pictureService.setIsCover(id);
        return JsonResult.ok();
    }

    @ApiOperation("撤销图片封面")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "图片id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/not-cover")
    public JsonResult<Void> setNotCover(@PathVariable Long id) {
        log.debug("开始处理[撤销封面]请求 参数:{}", id);
        pictureService.setNotCover(id);
        return JsonResult.ok();
    }

    @ApiOperation("修改图片信息")
    @ApiOperationSupport(order = 320)
    @ApiImplicitParam(name = "id", value = "图片id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable Long id, PictureUpdateDTO pictureUpdateDTO) {
        log.debug("开始处理[修改图片信息]请求 参数:{},{}", id, pictureUpdateDTO);
        pictureService.update(id, pictureUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("获取图片信息")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id", value = "图片id", required = true, dataType = "long")
    @GetMapping("/{id:[0-9]+}/get-by-id")
    public JsonResult<PictureStandardVO> getStandardById(@PathVariable Long id){
        log.debug("开始处理[获取图片信息]请求 参数:{}", id);
        return JsonResult.ok(pictureService.getStandardById(id));
    }

    @ApiOperation("获取图片列表")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParam(name = "albumId", value = "相册id", required = true, dataType = "long")
    @GetMapping("/{albumId:[0-9]+}/list-by-album")
    public JsonResult<List<PictureListItemVO>> listByAlbumId(@PathVariable Long albumId){
        log.debug("开始处理[获取图片信息]请求 参数:{}", albumId);
        return JsonResult.ok(pictureService.listByAlbum(albumId));
    }


}
